package com.derbysoft.synxisadapter.action.settings.channel;

import com.derbysoft.synxisadapter.action.BaseAdapterAction;
import com.derbysoft.synxisadapter.commons.model.Channel;
import com.derbysoft.synxisadapter.commons.model.TaxPolicy;
import com.derbysoft.synxisadapter.commons.repository.ChannelRepository;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

@Results(value = {
        @Result(name = Action.SUCCESS, location = "/page/settings/channel/Edit.ftl")
})
public class EditAction extends BaseAdapterAction {

    @Autowired
    private ChannelRepository channelRepository;

    private String passport;
    private String name;
    private String id;
    private String code;
    private String iataNumber;
    private String userName;
    private String password;
    private String taxPolicy;
    private boolean inclusiveTaxRate;

    public String execute() {
        Channel channel = channelRepository.findByPassport(passport);
        if (channel != null) {
            passport = channel.getPassport();
            name = channel.getName();
            id = channel.getId();
            code = channel.getCode();
            iataNumber = channel.getIataNumber();
            userName = channel.getUserName();
            password = channel.getPassword();
            taxPolicy = channel.getTaxPolicy().name();
            inclusiveTaxRate = channel.isInclusiveTaxRate();
        }
        return SUCCESS;
    }

    public String ajaxSubmit() {
        try {
            channelRepository.update(passport, name, id, code, iataNumber,userName,password, translate(taxPolicy), inclusiveTaxRate);
            return populateSucceededJsonResult(null);
        } catch (Exception e) {
            return populateFailedJsonResult(e);
        }
    }

    private TaxPolicy translate(String taxPolicy) {
        if (taxPolicy == null) {
            return TaxPolicy.NONE;
        }
        if (taxPolicy.equals(TaxPolicy.FIXED.name())) {
            return TaxPolicy.FIXED;
        }
        if (taxPolicy.equals(TaxPolicy.PERCENT.name())) {
            return TaxPolicy.PERCENT;
        }
        return TaxPolicy.NONE;
    }

    public String getPassport() {
        return passport;
    }

    public void setPassport(String passport) {
        this.passport = passport;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getIataNumber() {
        return iataNumber;
    }

    public void setIataNumber(String iataNumber) {
        this.iataNumber = iataNumber;
    }

    public String getTaxPolicy() {
        return taxPolicy;
    }

    public boolean isInclusiveTaxRate() {
        return inclusiveTaxRate;
    }

    public void setInclusiveTaxRate(boolean inclusiveTaxRate) {
        this.inclusiveTaxRate = inclusiveTaxRate;
    }

    public void setTaxPolicy(String taxPolicy) {
        this.taxPolicy = taxPolicy;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}